<template>
  <div class="dictionaryData h100">
    <object id="LODOP_OB" classid="clsid:2105C259-1E0C-4534-8141-A753534CB4CA" width=0 height=0> 
      <embed id="LODOP_EM" type="application/x-print-lodop" width=0 height=0></embed>
    </object>

      <div class="PageItem">

      <el-form :model="saveFormData" :rules="rules" ref="propertyForm" :inline="true" label-width="120px" :disabled="saveFlag">
        <el-row :gutter="5" style="height: 10px;"> </el-row>
        <el-row class="paddingleft2em" >
          <el-form-item label="　资产条码" style="margin-bottom: 0;">
            <el-input
              v-model="searchFormData.assetsNo"
              ref="assetsNo"
              :maxlength="20"
              placeholder="请扫描资产条码"
            >
            </el-input>
          </el-form-item>
          <el-button type="primary"   v-if="true || btnRight.btnCrematorMngAdd" @click="scanCode()">扫描</el-button>
          <el-button type="primary"  v-if="true || btnRight.btnCrematorMngDelete" @click="addProperty()">新增资产信息</el-button>

        </el-row>
        <el-row :gutter="5" style="height: 30px;"> </el-row>
        <el-row :gutter="5">
          <el-form-item label="资产编码:" prop="code" >
            <el-input v-model="saveFormData.assetsNo" @change="copyToAssetsCardNo(saveFormData.assetsNo)" maxlength="50"></el-input>
          </el-form-item>
          <el-form-item label="资产名称:" prop="assetsName" >
            <el-input v-model="saveFormData.assetsName"  maxlength="50"></el-input>
          </el-form-item>
          <el-form-item label="取得方式:" prop="getWayCode" >
            <el-select
              v-model="saveFormData.getWayCode"
              clearable
              filterable
              placeholder="取得方式"
              style="width:185px;"
            >
              <el-option
                v-for="item in copydictionary.get_way_code"
                :key="item.dataCode"
                :label="item.dataText"
                :value="item.dataCode"
                :disabled="item.disableState"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="资产类别:" prop="assetsCategoryCode" >
            <el-select
              v-model="saveFormData.assetsCategoryCode"
              clearable
              filterable
              placeholder="资产类别"
              style="width:185px;"
            >
              <el-option
                v-for="item in copydictionary.assets_category_code"
                :key="item.dataCode"
                :label="item.dataText"
                :value="item.dataCode"
                :disabled="item.disableState"
              />
            </el-select>
          </el-form-item>
        </el-row>
        <el-row :gutter="5">
          <el-form-item label="卡片序列号:" prop="assetsCardNo" >
            <el-input v-model="saveFormData.assetsCardNo" @change="copyToAssetsNo(saveFormData.assetsCardNo)" maxlength="50"></el-input>
          </el-form-item>
          <el-form-item label="取得日期:" prop="enableDate">
            <el-date-picker  v-model="saveFormData.enableDate" @change="getScrapDateByStart(saveFormData.enableDate)"
                             type="date"  placeholder="取得日期"   :picker-options="pickerOptions0" style="width:185px;">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="预计使用年限:" prop="assetslife" >
            <el-input v-model="saveFormData.assetsLife"  @change="getScrapDate(saveFormData.assetsLife)" :min="0" type="number"></el-input>
          </el-form-item>
          <el-form-item label="到期日期:" prop="scrapDate">
            <el-date-picker  v-model="saveFormData.scrapDate"   readonly="readonly"
                             type="date"  placeholder="到期日期"   style="width:185px;">
            </el-date-picker>
          </el-form-item>
        </el-row>
        <el-row :gutter="5"   >
          <el-form-item label="资产分类:" prop="assetsSortCode" >
            <el-select
              v-model="saveFormData.assetsSortCode"
              clearable
              filterable
              placeholder="资产分类"
              style="width:185px;"
            >
              <el-option
                v-for="item in copydictionary.assets_sort_code"
                :key="item.dataCode"
                :label="item.dataText"
                :value="item.dataCode"
                :disabled="item.disableState"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="累计折旧/摊销:" prop="promoteSum" >
            <el-input v-model="saveFormData.promoteSum"  type="number" maxlength="50"></el-input>
          </el-form-item>
          <el-form-item label="原值:" prop="originalValue" >
            <el-input v-model="saveFormData.originalValue" type="number" maxlength="50"></el-input>
          </el-form-item>
          <el-form-item label="净值:" prop="netValue" >
            <el-input v-model="saveFormData.netValue" type="number" maxlength="50"></el-input>
          </el-form-item>
        </el-row>
        <el-row :gutter="5">
          <el-form-item label="已计提期间数:" prop="extractNum" >
            <el-input v-model="saveFormData.extractNum" type="number"  maxlength="50"></el-input>
          </el-form-item>
          <el-form-item label="数量:" prop="assetsNum" >
            <el-input v-model="saveFormData.assetsNum"  type="number" onkeyup="this.value=this.value.replace(/\D|^0/g,'')" onafterpaste="this.value=this.value.replace(/\D|^0/g,'')" :min="0" maxlength="50"></el-input>
          </el-form-item>
          <el-form-item label="规格型号:" prop="specificationNo" >
            <el-input v-model="saveFormData.specificationNo"  maxlength="50"></el-input>
          </el-form-item>
        </el-row>
        <el-row>
          <el-form-item label="存放地点:"  prop="storageLoc" >
            <el-input  type="textarea" v-model="saveFormData.storageLoc" style="width: 503px;"></el-input>
          </el-form-item>
        </el-row>
        <el-row  class="paddingleft2em">
          <div style="margin-left: 400px;">
          <el-button type="primary"  v-if="true || btnRight.btnCrematorMngEdit" @click="saveProperty()">保存</el-button>
            <el-button type="primary"  v-if="true || btnRight.btnCrematorMngEdit" @click="printData()">打印资产条码</el-button></div>
        </el-row>
      </el-form>
    </div>
  </div>
</template>
<script>
  import * as API from '@/api/systemApplication'
  import {getLodop} from '@/utils/lodopFuncs'
  import {dataFormate} from '@/utils/index'
  export default {
    name: "propertyEdit",
    data() {
      return {
        rules: {
          assetsNo: [
            {required: true, message: '请填写', trigger: 'blur'},
          ],
          assetsName: [
            {required: true, message: '请填写', trigger: 'blur'},
          ],
          getWayCode: [
            {required: true, message: '请选择', trigger: 'change'},
          ],
          enableDate: [
            {required: true, message: '请选择', trigger: 'change'},
          ],
          assetsCardNo: [
            {required: true, message: '请填写', trigger: 'blur'},
          ],
          assetsLife: [
            {required: true, message: '请填写', trigger: 'blur'},
          ],
          scrapDate: [
            {required: true, message: '请选择', trigger: 'change'},
          ],
          assetsSortCode: [
            {required: true, message: '请选择', trigger: 'change'},
          ],
          promoteSum: [
            {required: true, message: '请填写', trigger: 'blur'},
          ],
          originalValue: [
            {required: true, message: '请填写', trigger: 'blur'},
          ],
          netValue: [
            {required: true, message: '请填写', trigger: 'blur'},
          ],
          extractNum: [
            {required: true, message: '请填写', trigger: 'blur'},
          ],
          assetsNum: [
            {required: true, message: '请填写', trigger: 'blur'},
          ],
          storageLoc: [
            {required: true, message: '请填写', trigger: 'blur'},
          ],
        },
        pickerOptions0: {
          disabledDate(time) {
            return time.getTime() > Date.now();
          }
        },
        searchFormData:{
          "assetsNo":""//资产编码
        },
        saveFormData:{
          //保存资产信息
          "id": 0,//主键
          "assetsNo":"",//资产编码
          "assetsName":"",//资产名称
          "getWayCode":"",//取得方式
          "enableDate":"",// 取得日期
          "assetsCardNo":"",//卡片序列号
          "assetsLife":0,//预计使用年限
          "scrapDate":"",// 到期日期
          "assetsSortCode ":1,//资产分类
          "assetsCategoryCode":1,//资产类别
          "promoteSum":"",//累计折旧/摊销
          "specificationNo":"",//规格型号
          "originalValue":0,//原值
          "netValue":0,// 净值
          "extractNum":0,//已计提期间
          "storageLoc":"",// 存放地点
          "assetsNum":0,//数量
          "version":"",//版本
        },
        dictionary:{},//字典值
        copydictionary:[],//字典值
        saveFlag:false, //保存标志
      }
    },
    methods: {
      onSubmit() {
        console.log('submit!');
      },//查询
      loadDictionaryData(){
        //没有数据，加载数据
        this.$store.dispatch("user/getdictionary")
          .then((res) => {
            //获取字典数据
            this.dictionary = res;
            this.copydictionary = JSON.parse(JSON.stringify(this.dictionary));
            this.$set(this.dictionary, res);
          })
          .catch(() => console.log("promise catch err"));
      },//扫描
      scanCode(){
        let postData=this.searchFormData;
        if(postData.assetsNo==''||postData.assetsNo==""){
          this.$message('请输入或扫描资产条码')
          return;
        }
        API['loadFixedAssetsInfoByAssetsNo'](postData).then(response=>{
          let { data }=response;
          if (!data) {
            this.$message('数据查询失败')
            return
          }
         this.saveFormData=data;
        }).catch(error => {
          this.$message(error)
        })
      },//新增
      addProperty(){
        let today=new Date();
        let sDay=new Date();
        sDay.setFullYear(today.getFullYear()+1);
        this.saveFormData={
          //保存资产信息
          "id": null,//主键
          "assetsNo":"",//资产编码
          "assetsName":"",//资产名称
          "getWayCode":1,//取得方式
          "enableDate":dataFormate(new Date()),// 取得日期
          "assetsCardNo":"",//卡片序列号
          "assetsLife":1,//预计使用年限
          "scrapDate":dataFormate(sDay),// 到期日期
          "assetsSortCode":1,//资产分类
          "assetsCategoryCode":1,//资产类别
          "promoteSum":0,//累计折旧/摊销
          "originalValue":0,//原值
          "netValue":0,// 净值
          "extractNum":0,//已计提期间
          "storageLoc":"",// 存放地点
          "assetsNum":0,//数量
          "version":"",//版本
        };
      },saveProperty(){
        //表单验证
        let this_save=this;
        this.$refs["propertyForm"].validate((valid) => {
          if (valid) {
            //提交数据
            let postData=this_save.saveFormData;
            //序列号与资产编号相同
               postData.assetsCardNo=postData.assetsNo;
            API['saveOrUpdateAssetsItems'](postData).then(response=>{
              if(response){
                let data=response;
                if (!data) {
                  this_save.$message('数据保存失败');
                  return
                }
                if(data.code==1){
                  this_save.$message('保存成功！');
                  this_save.clearForm();
                }
              }else{
                this_save.$message('数据保存失败');
              }
            }).catch(error => {
              // this.$message(error)
            });
          } else {
            console.log('error submit!!');
            return false;
          }
        });
       } ,clearForm(){
       this.addProperty();
      }
      , //打印二维码
      printData(){
        let this_print=this;
        if( this.searchFormData.assetsNo=='' ||this.searchFormData.assetsNo==""){
          this.$message("请输入资产编码");
        }else{
          //开始打印
          let postData={
            "assetsNo":this_print.searchFormData.assetsNo
          }
          API['loadFixedAssetsInfoByAssetsNo'](postData).then(response=>{
            let { data }=response;
            if (!data) {
              this.$message('数据查询失败')
              return
            }else{
              //打印二维码
              this_print.printCardNo(data);
            }
          }).catch(error => {
            this.$message(error)
          })
        }

      },
      //打印服务协议单
      printCardNo(data){
        let this_print=this;
        let LODOP =  getLodop();
        if (LODOP == null || !LODOP) {
          this_print.$message.alert('系统提示', '您的浏览器不支持打印控件，请用IE重试或安装打印控件后重试');
          return;
        }
        let assetsCardNo=data.assetsCardNo==null?"":data.assetsCardNo;
        let assetsName=data.assetsName==null?"":data.assetsName;
        let specificationNo=data.specificationNo==null?"":data.specificationNo;
        //纸张
        //LODOP.SET_PRINT_PAGESIZE(1, 0, 0, "A4");
        //设置字体
        LODOP.SET_PRINT_STYLE("FontSize",11);
        LODOP.ADD_PRINT_BARCODE(35, 275,70, 70, "QRCode",assetsCardNo);
        LODOP.ADD_PRINT_TEXT(30,50,270,25,"资产名称："+assetsName);
        LODOP.ADD_PRINT_HTM(60,50,270,25,"规格型号："+specificationNo);
        LODOP.ADD_PRINT_TEXT(90,50,270,25,"资产条码："+assetsCardNo);
        //调用打印控件进行测试
        //打印边框
        //直接打印
        //打印预览
        LODOP.PREVIEW();
        //LODOP.PRINT();
        //LODOP.PRINT_DESIGN();
      },//通过取得日期获得到期日期
      getScrapDateByStart(value){
        if(this.saveFormData.assetsLife==""||this.saveFormData.assetsLife==null||this.saveFormData.assetsLife<0){
          return ;
        }
          let startObj = value ;
          let startDateStr;
          try{
            if(startObj &&  startObj.getTime()>0 ){
              startDateStr=dataFormate(startObj);
              startDateStr=startDateStr.substring(0,10);
            }
          }catch(e){
            if(startObj.length>=10){
              startDateStr=this.saveFormData.enableDate.substring(0,10);
            }else{
              this.$message("请先录入取得日期");
              return;
            }
          }
        let aDate = startDateStr.split('-');
        let startDate = new Date(aDate[1]+'-'+aDate[2]+'-'+aDate[0]) ;
        let addYear=this.saveFormData.assetsLife;
        if(addYear<0){
          this.saveFormData.assetsLife=0;
          return ;
        }else{
          let sYear=startDate.getFullYear();
          sYear= Number(sYear)+Number(addYear);
          startDate.setFullYear(sYear);
          this.saveFormData.scrapDate=startDate;
        }

      },//获得到期年限
      getScrapDate(value){
        if(this.saveFormData.enableDate==""||this.saveFormData.enableDate==null){
          this.$message("请先录入取得日期");
          return ;
        }
        let startObj=this.saveFormData.enableDate;
        let startDateStr;
        try{
          if(startObj &&  startObj.getTime()>0 ){
            startDateStr=dataFormate(startObj);
            startDateStr=startDateStr.substring(0,10);
          }
        }catch(e){
          if(startObj.length>=10){
            startDateStr=this.saveFormData.enableDate.substring(0,10);
          }else{
            this.$message("请先录入取得日期");
            return;
          }
        }
        let aDate = startDateStr.split('-');
        let startDate = new Date(aDate[1]+'-'+aDate[2]+'-'+aDate[0]) ;
        let addYear=this.saveFormData.assetsLife;
        if(addYear<0){
          this.saveFormData.assetsLife=0;
          return ;
        }else{
          let sYear=startDate.getFullYear();
          sYear= Number(sYear)+Number(addYear);
          startDate.setFullYear(sYear);
          this.saveFormData.scrapDate=startDate;
        }

      },copyToAssetsCardNo(value){
        this.saveFormData.assetsCardNo=value;
      },copyToAssetsNo(value){
        this.saveFormData.assetsNo=value;
      },scanOpen(){
        console.log();
        if(this.$route.params.assetsNo){
          this.searchFormData.assetsNo=this.$route.params.assetsNo;
          this.scanCode();
        }

      },
    } ,mounted(){
      //加载字典寄存目录类型
      this.loadDictionaryData();
      // debugger;
      console.log(this.$route,444)
      //载入按钮
      // this.btnRight = this.$route.meta.buttons;
      // //遍历属性赋值将true与false字符串转换成布尔型的值
      // for (let key of Object.keys(this.btnRight)) {
      //   this.btnRight[key]= this.btnRight[key]=(this.btnRight[key]|| this.btnRight[key]=="true")?true:false;
      // }
    },watch:{
      '$route': {
        immediate: true,//深度监听
        deep: true,
        handler (val, oldVal) {
          this.scanOpen();
        }
      }
    },

  }
</script>
<style scoped>

</style>
